﻿using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Input;
using AzureDiagMon.Common;
using AzureDiagMon.Common.Dtos;
using AzureDiagMon.Common.Interfaces.Services;
using AzureDiagMon.Common.Mvvm;
using AzureDiagMon.Common.Mvvm.Interfaces;
using AzureDiagMon.Common.Services;
using AzureDiagMon.Services;
using AzureDiagMon.ViewModels.Entities;
using AzureDiagMon.ViewModels.EntityEditors;
using GalaSoft.MvvmLight.Command;
using Ninject;

namespace AzureDiagMon.ViewModels
{

    public class MainViewModel : ViewModel
    {
        public string AppTitle
        {
            get
            {
                //TODO: AppTitle!
                return ""; // Resources.AppTitle; } }
            }
        }

        #region Imported services

        [Inject]
        public IDataStoreService DataStoreService { get; private set; }

        [Inject]
        //TODO: Kell ez?
        //        [Import(RequiredCreationPolicy = CreationPolicy.Shared)]
        public LogSyncService LogSyncService { get; private set; }

        #endregion

        #region Data

        private ObservableCollection<StorageAccountViewModel> storageAccounts;
        public ObservableCollection<StorageAccountViewModel> StorageAccounts
        {
            get { return storageAccounts; }
            set
            {
                storageAccounts = value;
                RaisePropertyChanged("StorageAccounts");
            }
        }

        private StorageAccountViewModel selectedStorageAccount;
        public StorageAccountViewModel SelectedStorageAccount
        {
            get { return selectedStorageAccount; }
            set
            {
                selectedStorageAccount = value;
                RaisePropertyChanged("SelectedStorageAccount");
            }
        }

        #endregion

        #region Commands

        public ICommand AddStorage { get; private set; }

        public ViewModel Screens { get; private set; }

        public ICommand KeyDownEventCommand { get; private set; }

        #endregion

        #region Startup and shutdown

        public MainViewModel()
        {
            if (IsInDesignMode)
            {
            }
            else
            {
                LoadData();
                LogSyncService.Start();
            }

            AddStorage = (ICommand)new RelayCommand(
                () =>
                {
                    Debug.WriteLine("Add Storage Command called");

                    var vm = new StorageAccountEditorViewModel(new StorageAccountDto());
                    if (ShowDialog(vm))
                    {
                        DataStoreService.AddStorageAccount(vm.Entity);
                        LoadData();
                    }
                });

            KeyDownEventCommand = new RelayCommand<KeyEventArgs>(
                (args) =>
                {
                    switch (args.Key)
                    {
                    }
                });

        }

        public override void Cleanup()
        {
            LogSyncService.Stop();

            base.Cleanup();
        }

        #endregion

        private void LoadData()
        {
            StorageAccounts = new ObservableCollection<StorageAccountViewModel>(DataStoreService.ListStorageAccounts().Select(f => new StorageAccountViewModel(f)));
            SelectedStorageAccount = StorageAccounts.FirstOrDefault();
            LogSyncService.Configuration = new LogSyncServiceConfiguration(SelectedStorageAccount.Entity, new TimeSpan(0, 0, 0, 30));
        }

    }
}